﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace working
{
    public class CmdAgent
    {
        private Process proc = null;
        /// <summary>
        /// 构造方法
        /// </summary>
        public CmdAgent()
        {
            proc = new Process();
        }
        /// <summary>
        /// 执行CMD语句
        /// </summary>
        /// <param name="cmd">要执行的CMD命令</param>
        public string RunCmd(string cmd)
        {
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.FileName = "cmd.exe";
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.RedirectStandardInput = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.Start();
            WriteEnvrioments(proc.StandardInput);
            proc.StandardInput.WriteLine(cmd);
            proc.StandardInput.WriteLine("exit");
            string outStr = proc.StandardOutput.ReadToEnd();
            proc.Close();
            return outStr;
        }

        public void RunCmd(string cmd,Action<string> onCmdLine)
        {
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.FileName = "cmd.exe";
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.RedirectStandardInput = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.Start();
            WriteEnvrioments(proc.StandardInput);
            proc.StandardInput.WriteLine(cmd);
            proc.StandardInput.WriteLine("exit");
            if (onCmdLine != null)
            {
                try
                {
                    onCmdLine(proc.StandardOutput.ReadLine());
                    while (!proc.StandardOutput.EndOfStream)
                    {
                        onCmdLine(proc.StandardOutput.ReadLine());
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
            proc.WaitForExit();
            proc.Close();
        }
        /// <summary>
        /// 打开软件并执行命令
        /// </summary>
        /// <param name="programName">软件路径加名称（.exe文件）</param>
        /// <param name="cmd">要执行的命令</param>
        public void RunProgram(string programName, string cmd="")
        {
            Process proc = new Process();
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.FileName = programName;
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.RedirectStandardInput = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.Start();
            if (cmd.Length != 0)
            {
                WriteEnvrioments(proc.StandardInput);
                proc.StandardInput.WriteLine(cmd);
            }
            proc.Close();
        }

        protected void WriteEnvrioments(StreamWriter writer)
        {
            string dir = System.Environment.CurrentDirectory;//获取当前文件目录
            string iniPath = $"{dir}/envrioment.txt";
            if (File.Exists(iniPath))
            {
                var lines = System.IO.File.ReadAllLines(iniPath);
                foreach (var line in lines)
                {
                   var pair = line.Split('=');
                    if (pair.Length <= 1)
                        continue;
                    Console.WriteLine("{pair[0]}={ pair[1]}");
                    writer.WriteLine($"set {pair[0]}={pair[1]}");
                }
            }
            else
            {
                System.IO.File.WriteAllText(iniPath, "x=100");
            }
        }
    }
}
